<?php
namespace app\api\controller;
/**
 *backup system
 */
use think\Controller;
use think\Db;
use think\Cache;
use app\api\model\ApiAuthorizationModel;
use app\admin\model\AdminMenuModel;
use app\admin\model\AccessModel;
use app\admin\model\ModuleMenuModel;
use app\api\model\ApiAccessAuthorityModel;
use app\api\controller\ApiAccessAuthorityController;
use plugins\appmarket\model\AdminIndexModel as AppmarketAdminIndexModel;
use plugins\appmarket\controller\ConfigController as AppmarketConfigController; 
use plugins\appmarket\model\ModulePurchaseHistoryModel; 

use app\api\controller\ApiAccessEncodeController;


/**
 * 
 */
class ApiBackupController extends Controller
{
	//file delete path
	const deleteFilePath = '/api/backup/deleteFile';
	
	protected $apiaccessObj = null;
	public function  _initialize()
    {
        $this->apiaccessObj = new ApiAccessEncodeController();
    }
	
	
	//s
	public function getBackupList()
	{
		$this->checkInfo();
		$list = Db::name('company_backup_records')->where( 'status' , 2 )->where( 'addr' , "<>" ,'' )->select()->toArray();
		return zy_json_echo(true, 'success' , $list);
	}
	
	public function backupStatusReturn()
	{
		$param = $this->request->param();
		$this->checkInfo();
		if( !isset( $param['bkid'] ) || empty( $param [ 'bkid' ] ) ){
			return zy_json_echo(false , 'bkid is undefined');
		}
		if( !isset( $param['status'] ) || empty( $param [ 'status' ] ) ){
			return zy_json_echo(false , 'status is undefined');
		}
		
		$update = [
			'status' => ( $param['status'] == 1 )  ?  4 : 3 ,
			'bak_time' => time()
		];		
		$where = [
			'id' => $param["bkid"]
		];
		$res = Db::name( "company_backup_records" )->where( $where )->update( $update );
		if( !$res ){
			return zy_json_echo( false , 'update failed' );
		}
		return zy_json_echo( true , 'update success' );
	
	}
	
	
	//deleteFile
	public function deleteFile( $companyId ,  $filename )
	{
		$this->checkInfo();
		$find = Db::name( 'company' )->where( 'id' , $companyId )->find();
		$url = $find[ 'commicate_addr' ]."/".self::deleteFilePath;
		
		$pm = [
			'cid' => $companyId,
			'filename' => $filename
		];
		$pm = $this->apiaccessObj->signCode($pm);
		$pm = http_build_query( $pm );
		
		$url .= "?".$pm;
		$res = cmf_curl_get( $url );
		$res = json_decode( $res , true );
		if( isset( $res [ 'status' ]  )  && $res [ 'status' ] == "success" ){
			return zy_json_echo(true , "sucess".$url);
		}
		return zy_json_echo(false , 'failed');
	}
	
	
	
	// get sign info 
	public function checkInfo ( $updateIp=0 )
	{
		$data = $this->request->param();
		if ($updateIp == 0 ) {
			$this->checkIp();
		}
		
		if( !isset( $data [ 'a' ] ) || empty( $data [ 'a' ] ) ){
			return zy_json_echo( false , 'a is not defined' );
		}
		if( !isset( $data [ 'c' ] ) || empty( $data [ 'c' ] ) ){
			return zy_json_echo( false , 'c is not defined' );
		}
		
		$company = Db::name( 'company' )->where('aid' , '==='.$data[ 'a' ] )->find();
		$key = substr(md5($company['id'].$company['create_time']),10,20);
		if( $key != $data ['c'] ){
			return zy_json_echo(false , 'Permission to fail' );
		}
		true;
	}
	
	//create sign
	public function getSecrect(){
		$this->checkInfo();
		$data = $this->request->param();
		unset( $data [ 'a' ] ,$data['c']  );
		$str = $this->apiaccessObj->signCode( $data );
		return zy_json_echo(true , '' , http_build_query($str) , "200" );
		//echo http_build_query($str);exit;
	}
	
	
	//get location 
	private function getIp()
	{
		if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
			$ip = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
			$ip = trim(current($ip));
		}else{
			$ip = $_SERVER['REMOTE_ADDR'];
		}
		
		return $ip;
	}
	
	private function checkIp(){
		$where = [
			'company_id' => 8 ,
			'status' => 8 ,
			'backup_type' => 8
		];
		$find = Db::name( 'company_backup_records' )->where( $where )->find();
		$ip = $find['addr'];
		
		$visitIp = $this->getIp();
		if($ip != $visitIp ){
			return zy_json_echo(false , "非法请求被拒绝！");
		}
	}
	
	//update addrInfo 
	public function k()
	{
		$this->checkInfo(1);
		
		$where = [
			'company_id' => 8 ,
			'status' => 8 ,
			'backup_type' => 8
		];
		$find = Db::name( 'company_backup_records' )->where( $where )->find();
		
		if( empty ( $find ) ){
			$data = [
				'company_id' => 8,
				'operator' => '卓远',
				'operator_id' => 1,
				'status' => 8,
				'backup_type' => 8,
				'remarks' => '记录不要删除,update time :'.date('Y-m-d H:i:s',time()),
				'bak_time' => time()
			];
			$id = Db::name( 'company_backup_records' )->insert( $data );
		}else{
			$id = $find[ 'id' ];
		}
		$ip = $this->getIp();

		if( $ip != $find['addr'] ){
			$res = Db::name( 'company_backup_records' )->where( 'id' , $id )->update( [ 'addr' => $ip , 'bak_time'=>time() ] );
			if( !$res ){
				echo "is failed";exit;
			}
			echo "is success" ; exit;
		}
		echo "nothing to do";exit;
	}
	
	
	
	
}